package cn.flying.cloud.flow.audit.controller;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import cn.flying.cloud.flow.annotation.TaskDefKey;

/**
 * @author: admin
 * @date: 2025年06月24日 15:15
 * @version: 1.0
 */
public abstract class BaseFlowAuditController implements InitializingBean, DisposableBean {

    private final Map<String, Method> methodCache = new ConcurrentHashMap<>();

    public Method getMethod(String taskDefKey) {
        return methodCache.get(taskDefKey);
    }

    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        Class<? extends BaseFlowAuditController> clazz = this.getClass();
        for (Method method : clazz.getDeclaredMethods()) {
            // 检查方法是否有TaskDefKey注解
            if (method.isAnnotationPresent(TaskDefKey.class)) {
                TaskDefKey annotation = method.getAnnotation(TaskDefKey.class);
                String key = annotation.value();

                // 确保注解值不为空
                if (!key.isEmpty()) {
                    // 设置方法可访问（如果是私有方法）
                    method.setAccessible(true);
                    // 缓存方法
                    methodCache.put(key, method);
                }
            }
        }
    }

    /**
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        methodCache.clear();
    }
}
